package com.zzl.bonds.ticket.manager;

import com.zzl.bonds.ticket.entity.*;
import com.zzl.bonds.ticket.service.TicketFlowChartService;
import com.zzl.bonds.ticket.service.TicketService;
import com.zzl.bonds.ticket.untils.DateUtils;
import com.zzl.bonds.ticket.untils.StrUtils;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Arrays;

/**
 * @author ：hyt
 * @description：票据流程图通用处理
 * @date ：2022/1/10 14:50
 */
@Component
public class TicketFlowChartManager {
    @Autowired
    private TicketFlowChartService ticketFlowChartService;

    @Autowired
    private TicketService ticketService;

    @Transactional(rollbackFor = Exception.class)
    public void saveFlowChartForOpen(TicketOpenEntity oldOpenEntity, String ticketId, User user) throws SysException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtils.DF_YYYY_MM_DD);
        TicketFlowChartEntity ticketFlowChartEntity = new TicketFlowChartEntity();
        ticketFlowChartEntity.setTitle("电子债权凭证 开立");
        ticketFlowChartEntity.setTicketNo("凭证编号：" + oldOpenEntity.getTicketNo());
        ticketFlowChartEntity.setInitiatorEnterpriseName("开单企业：" + oldOpenEntity.getOpenEnterpriseName());
        ticketFlowChartEntity.setFirstTime("开单日期：" + simpleDateFormat.format(oldOpenEntity.getInvoiceTime()));
        ticketFlowChartEntity.setMoneyAmountFirst("凭证金额：" + StrUtils.simgleFormat(oldOpenEntity.getTicketAmount()) + "元");
        ticketFlowChartEntity.setSignEnterpriseName("收单企业：" + oldOpenEntity.getReceiveEnterpriseName());
        ticketFlowChartEntity.setParentId("0");
        ticketFlowChartEntity.setType("2");
        ticketFlowChartEntity.setApplyId(oldOpenEntity.getId());
        ticketFlowChartEntity.setTicketOpenId(oldOpenEntity.getId());
        ticketFlowChartEntity.setCreatorId(user.getId());
        ticketFlowChartEntity.setTicketId(ticketId);
        if (!ticketFlowChartService.save(ticketFlowChartEntity)) {
            throw new SysException("A00001", "保存失败！");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveFlowChartForTransfer(TicketransferEntity ticketTransferEntity, String ticketOpenId, Integer createId) throws SysException {
        TicketFlowChartEntity lastIdForTicketId = ticketFlowChartService.getLastIdForTicketId(ticketTransferEntity.getParentTicketId());
        TicketEntity fatherTicket = ticketService.getById(ticketTransferEntity.getParentTicketId());
        //设置转让出的票
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtils.DF_YYYY_MM_DD);
        TicketFlowChartEntity ticketFlowChartEntity = new TicketFlowChartEntity();
        ticketFlowChartEntity.setTitle("拆转子单");
        ticketFlowChartEntity.setTicketNo("凭证编号：" + ticketTransferEntity.getTicketNo());
        ticketFlowChartEntity.setInitiatorEnterpriseName("转单企业：" + ticketTransferEntity.getTransferEnterpriseName());
        ticketFlowChartEntity.setFirstTime("转单日期：" + simpleDateFormat.format(ticketTransferEntity.getCreateTime()));
        ticketFlowChartEntity.setMoneyAmountFirst("凭证金额：" + StrUtils.simgleFormat(ticketTransferEntity.getTransferAmount()) + "元");
        ticketFlowChartEntity.setSignEnterpriseName("收单企业：" + ticketTransferEntity.getReceiveEnterpriseName());
        ticketFlowChartEntity.setApplyId(ticketTransferEntity.getId());
        ticketFlowChartEntity.setParentId(lastIdForTicketId.getId());
        ticketFlowChartEntity.setCreatorId(createId);
        ticketFlowChartEntity.setType("0");
        ticketFlowChartEntity.setTicketId(ticketTransferEntity.getTicketId());
        ticketFlowChartEntity.setTicketOpenId(ticketOpenId);
        if (!ticketFlowChartService.save(ticketFlowChartEntity)) {
            throw new SysException("A00001", "保存失败！");
        }

        //处理留存子单信息
        if (StringUtils.isNotBlank(ticketTransferEntity.getTicketRetentionNo())) {
            //设置票据的剩余金额
            TicketFlowChartEntity ticketFlowChartTwo = new TicketFlowChartEntity();
            ticketFlowChartTwo.setTitle("留存子单");
            ticketFlowChartTwo.setTicketNo("凭证编号：" + ticketTransferEntity.getTicketRetentionNo());
            ticketFlowChartTwo.setInitiatorEnterpriseName(lastIdForTicketId.getInitiatorEnterpriseName());
            ticketFlowChartTwo.setFirstTime(lastIdForTicketId.getFirstTime());
            ticketFlowChartTwo.setSignEnterpriseName(lastIdForTicketId.getSignEnterpriseName());
            ticketFlowChartTwo.setMoneyAmountFirst("凭证金额：" + StrUtils.simgleFormat(ticketTransferEntity.getTicketRetentionAmount()) + "元");
            ticketFlowChartTwo.setParentId(lastIdForTicketId.getId());
            //3转让产生的票数据
            ticketFlowChartTwo.setType("3");
            ticketFlowChartTwo.setApplyId(ticketTransferEntity.getId());
            ticketFlowChartTwo.setCreatorId(createId);
            ticketFlowChartTwo.setTicketId(ticketTransferEntity.getTicketRetentionId());
            ticketFlowChartTwo.setTicketOpenId(ticketOpenId);
            if (!ticketFlowChartService.save(ticketFlowChartTwo)) {
                throw new SysException("A00001", "保存失败！");
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveFlowChartForFinance(TicketFinanceEntity financeEntity, Integer createId) throws SysException {
        TicketFlowChartEntity lastIdForTicketId = ticketFlowChartService.getLastIdForTicketId(financeEntity.getTicketId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtils.DF_YYYY_MM_DD);
        TicketFlowChartEntity ticketFlowChartEntity = new TicketFlowChartEntity();
        ticketFlowChartEntity.setTitle("拆融融资子单");
        ticketFlowChartEntity.setTicketNo("凭证编号:" + financeEntity.getTicketNo());
        ticketFlowChartEntity.setInitiatorEnterpriseName("融资企业：" + financeEntity.getFinanceEnterpriseName());
        ticketFlowChartEntity.setFirstTime("融单日期：" + simpleDateFormat.format(financeEntity.getCreateTime()));
        ticketFlowChartEntity.setMoneyAmountFirst("凭证金额：" + StrUtils.simgleFormat(financeEntity.getTicketAmount()) + "元");
        ticketFlowChartEntity.setSignEnterpriseName("融资机构：" + financeEntity.getInstinctyName());
        ticketFlowChartEntity.setApplyId(financeEntity.getId());
        ticketFlowChartEntity.setParentId(lastIdForTicketId.getId());
        ticketFlowChartEntity.setType("1");
        ticketFlowChartEntity.setTicketId(financeEntity.getTicketId());
        ticketFlowChartEntity.setCreatorId(createId);
        if (!ticketFlowChartService.save(ticketFlowChartEntity)) {
            throw new SysException("A00001", "保存失败！");
        }

        if (StringUtils.isNotBlank(financeEntity.getRetentionTicketId())) {
            TicketFlowChartEntity retainedFlowChart = new TicketFlowChartEntity();
            TicketEntity ticketEntity = ticketService.getById(financeEntity.getRetentionTicketId());

            retainedFlowChart.setTitle("拆融留存子单");
            retainedFlowChart.setTicketNo("凭证编号:" + financeEntity.getRetentionTicketNo());
            if (Arrays.asList(1, 5, 6).contains(ticketEntity.getTicketTypeSource())) {
                retainedFlowChart.setInitiatorEnterpriseName("开单企业：" + ticketEntity.getOpenEnterpriseName());
                retainedFlowChart.setFirstTime("开单日期：" + simpleDateFormat.format(ticketEntity.getInvoiceTime()));
                retainedFlowChart.setSignEnterpriseName("收单企业：" + ticketEntity.getEnterpriseName());
            } else {
                retainedFlowChart.setInitiatorEnterpriseName("转单企业：" + ticketEntity.getOpenEnterpriseName());
                retainedFlowChart.setFirstTime("转单日期：" + simpleDateFormat.format(ticketEntity.getCreateTime()));
//                retainedFlowChart.setSignEnterpriseName("收单企业：" + ticketEntity.getReceiveEnterpriseName());
            }
            retainedFlowChart.setMoneyAmountFirst("凭证金额：" + StrUtils.simgleFormat(ticketEntity.getTicketSurplusAmount()) + "元");
            retainedFlowChart.setSignEnterpriseName("融资机构：" + financeEntity.getInstinctyName());
            retainedFlowChart.setApplyId(financeEntity.getId());
            retainedFlowChart.setParentId(lastIdForTicketId.getId());
            retainedFlowChart.setType("4");
            retainedFlowChart.setTicketId(financeEntity.getRetentionTicketId());
            retainedFlowChart.setCreatorId(createId);
            if (!ticketFlowChartService.save(retainedFlowChart)) {
                throw new SysException("A00001", "保存失败！");
            }
        }
    }


}
